#include "drawpanel.h"
#include "appsettings.h"

#include <QPainter>
#include <QDebug>

DrawPanel::DrawPanel(QWidget *parent) :
    QWidget(parent)
{
    discreteness = 8;
    scale = 1.0;
    toolBox = NULL;
    scheme = NULL;
    setFixedSize(800, 600);
    // Установка фона
    QPalette p(palette());
    p.setColor(QPalette::Background, Qt::white);
    p.setColor(QPalette::Shadow, Qt::black);
    setPalette(p);
    setAutoFillBackground(true);
}

DrawPanel::~DrawPanel()
{

}

//Отрисовка холста
void DrawPanel::paintEvent(QPaintEvent *e)
{
    QPainter painter;
    painter.begin(this);
    //Рисуем рамку
    painter.drawRect(0,0,width()-1,height()-1);

    {
        int ex = 1, ey = 1;
        static const double step = 13.0;
        int i;
        double x,y;
        QPen pen;
        pen.setWidth(1);
        pen.setColor(QColor(140, 140, 140));

        QBrush brush;
        brush.setColor(QColor(250, 250, 100));

        painter.setBrush(brush);
        painter.setPen(pen);

        //painter.fillRect(event->rect(), QColor(220, 220, 200));
        for(i = 0, x = 0.0; x < width(); x=step*++i){
            painter.drawLine(x, 0, x, height());
            if( (i%10)==0 )
            {
                painter.drawLine(x+1, 0, x+1, height());
            }
        }

        for(i = 0, y = 0.0; y < height(); y=step*++i){
            painter.drawLine(0, y, width(), y);
            if( (i%10)==0 )
            {
                painter.drawLine(0, y+1, width(), y+1);
            }
        }

        pen.setWidth(3);
        pen.setColor(QColor(160, 80, 80));
        painter.setPen(pen);
        painter.fillRect(step*ex, step*ey, step*3, step*4, QColor(220, 220, 200));
        painter.drawRect(step*ex, step*ey, step*3, step*4);
        painter.drawLine(step*(ex-1), step*(ey+1), step*ex, step*(ey+1));
        painter.drawLine(step*(ex-1), step*(ey+3), step*ex, step*(ey+3));
        painter.drawLine(step*(ex+3), step*(ey+2), step*(ex+4), step*(ey+2));
    }

    if (scheme != NULL)
    {
        //Рисуем приветствие
        if (scheme->elements.count() == 0)
        {
            QPen p;
            p.setColor(Qt::blue);
            painter.setPen(p);
            painter.setFont(QFont(AppSettings::getInstance().defaultFontFamily,14));
            painter.drawText(10,30,tr("This scheme is empty."));
            painter.drawText(10,50,tr("Just create elements!"));
        }
        //Рисуем все элементы схемы
        for(int i = 0; i < scheme->elements.count(); i++)
        {
            /* WTF ?
            ElementImage* teml = new ElementImage();
            teml->create(100,100);
            delete teml;*/

            ElementImage* img = scheme->elements.at(i)->getImage();
            if (img != NULL)
            {
                qDebug() << img << endl;
                img->paintToBuffer(scale);
                painter.drawImage(img->left,img->top,*img->getBufferImage());
            }
        }
        //Запустить отрисовку инструмента
        if (toolBox != NULL)
        {
            DrawingTool* tool = toolBox->activeTool();
            if (tool != NULL)
                tool->onPaintScheme(this,painter);
        }
    }

    painter.end();
}

void DrawPanel::mouseMoveEvent(QMouseEvent *e)
{
    qDebug() << "event";
}

void DrawPanel::mousePressEvent(QMouseEvent *e)
{

}

void DrawPanel::mouseReleaseEvent(QMouseEvent *e)
{

}

void DrawPanel::setScheme(MicroScheme *scheme)
{
    this->scheme = scheme;
}

void DrawPanel::initToolBox(IToolBox *toolbox)
{
    toolBox = toolbox;
}
